package com.a.module_login

import com.example.myframe.DistDeptApp
import com.hjq.http.EasyHttp
import com.hjq.http.config.IRequestApi
import com.hjq.http.listener.OnHttpListener
import com.hjq.http.request.GetRequest
import com.a.lib_base.base.viewmodel.BaseNetStateViewModel
import com.a.lib_bean.api.ParamsApi
import com.a.lib_bean.api.Url
import com.a.lib_bean.bean.Employee
import com.a.lib_bean.bean.EmployeeBean
import com.a.lib_bean.enums.PermissionType
import com.a.lib_network.bean.HttpData
import com.a.lib_network.bean.NetState
import com.a.lib_network.enums.NetStateEnum
import com.a.lib_network.http.LiveDataHttpCallBack
import kotlin.coroutines.suspendCoroutine

class LoginViewModel : BaseNetStateViewModel() {

    fun get(type: PermissionType, username: String, password: String) {
        val params: MutableMap<String, String> = HashMap()
        params["username"] = username
        params["password"] = com.a.lib_utils.ToolsUtils.md5(password)
        EasyHttp.get(owner).api(ParamsApi(Url.doLogin(type), params)).request(
            object : LiveDataHttpCallBack<HttpData<EmployeeBean>>(_liveData) {
                override fun succeed(result: HttpData<EmployeeBean>) {
                    if (!result.isSuccess) {
                        _liveData.value =
                            NetState(result.msg, NetStateEnum.FAIL)
                        return
                    }
                    val employee: Employee = result.data.employee
                    DistDeptApp.setEmployee(employee)
                    _liveData.value = NetState(NetStateEnum.SUCCESS)
                }
            })

    }

}